﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class infomanage : System.Web.UI.Page
{
    string name1 = "";
    string field1 = "";
    string district1 = "";
    string type1 = "";
    string size1 = "";
    string email1 = "";
    string phone1 = "";
    string qq1 = "";
    string id1 = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null)
        {
            Response.Redirect("error_log.aspx", true);
        }
        else
        {
            if (Session["IDENTITY"].Equals("employee") == true)
            {
                Response.Redirect("error_auth.aspx", true);
            }
            string username = Session["USERNAME"].ToString();
           
            
            //在INFO_ENTER中查询用户名为username的用户信息，设置为每个textbox初始显示的值
            SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc.Open();
            SqlCommand com1 = new SqlCommand();
            com1.Connection = sc;
            com1.CommandText = "select ENTER_NAME,ENTER_FIELD,ENTER_DISTRICT,ENTER_TYPE,ENTER_SIZE,ENTER_EMAIL,ENTER_PHONE,ENTER_QQ from INFO_ENTER where ENTER_ID=@eid";
            com1.Parameters.AddWithValue("@eid",Session["ENTERID"].ToString());
            SqlDataReader sr = com1.ExecuteReader();
            while (sr.Read())
            {
                name1 = sr[0].ToString();
                field1 = sr[1].ToString();
                district1 = sr[2].ToString();
                type1 = sr[3].ToString();
                size1 = sr[4].ToString();
                email1 = sr[5].ToString();
                phone1 = sr[6].ToString();
                object temp_7 = sr[7];
                if (temp_7 != null)
                    qq1 = sr[7].ToString();
               
            }
            
            if (!IsPostBack)
            {
                TextBox1.Text = name1;
                TextBox6.Text = email1;
                TextBox7.Text = phone1;
                TextBox8.Text = qq1;
                DropDownList1.SelectedValue = district1;
                DropDownList2.SelectedValue = type1;
                DropDownList3.SelectedValue = field1;
                DropDownList4.SelectedValue = size1;
            }
                sc.Close();


        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
         //将原信息和按键触发时textbox中的数据比较，将有改动的更新进INFO_ENTER表，弹窗提示
        if (TextBox1.Text.Equals(name1) == true && TextBox6.Text.Equals(email1) == true && TextBox7.Text.Equals(phone1) == true && TextBox8.Text.Equals(qq1) == true && DropDownList1.SelectedValue.Equals(district1) == true && DropDownList2.SelectedValue.Equals(type1) == true && DropDownList3.SelectedValue.Equals(field1) == true && DropDownList4.SelectedValue.Equals(size1) == true)
        {
            Response.Write("<script language=\"javascript\" type=\"text/javascript\">alert('信息无改动！');</script>");
        }
        else
        {
            SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc1.Open();
            
            SqlCommand com2 = new SqlCommand();
            com2.Connection = sc1;
            com2.CommandText = "update INFO_ENTER set ENTER_NAME=@name,ENTER_FIELD=@field,ENTER_EMAIL=@email,ENTER_PHONE=@phone,ENTER_QQ=@qq,ENTER_DISTRICT=@district,ENTER_TYPE=@type,ENTER_SIZE=@size where ENTER_ID=@id";
            com2.Parameters.AddWithValue("@name",TextBox1.Text);
             com2.Parameters.AddWithValue("@field",DropDownList3.Text);
             com2.Parameters.AddWithValue("@email",TextBox6.Text);
             com2.Parameters.AddWithValue("@phone",TextBox7.Text);
             com2.Parameters.AddWithValue("@qq",TextBox8.Text);
             com2.Parameters.AddWithValue("@district",DropDownList1.Text);
             com2.Parameters.AddWithValue("@type",DropDownList2.Text);
             com2.Parameters.AddWithValue("@size",DropDownList4.Text);
             com2.Parameters.AddWithValue("@id",Session["ENTERID"].ToString());
             while (Application["sqllock"] == "lock")
             {//检测是否已被锁住
                 System.Threading.Thread.Sleep(2000);
             }
             Application["sqllock"] = "lock";//上锁          

            
             if (com2.ExecuteNonQuery() == 1)
             { 
                 Application["sqllock"] = "unlock";//解锁
                 Session["USERNAME"] = TextBox1.Text;
                 Response.Write("<script language=\"javascript\" type=\"text/javascript\">alert('信息成功提交！');</script>");
             }
             else
             {                        
                 Application["sqllock"] = "unlock";//解锁
                 Response.Write("<script language=\"javascript\" type=\"text/javascript\">alert('数据库出错，请重新修改！');</script>");
             }

        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("infopassword.aspx");
    }
}
